package com.woniuxy.movie.cinema.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.movie.cinema.dao.CinemaHallMapper;
import com.woniuxy.movie.cinema.dao.CinemaSeatMapper;
import com.woniuxy.movie.cinema.exception.CinemaException;
import com.woniuxy.movie.cinema.form.cinemaseat.SelectHallSeatForm;
import com.woniuxy.movie.cinema.form.cinemaseat.updateSeatStateForm;
import com.woniuxy.movie.cinema.model.CinemaHall;
import com.woniuxy.movie.cinema.model.CinemaSeat;
import com.woniuxy.movie.cinema.service.CinemaSeatService;
import com.woniuxy.movie.commons.web.util.StateData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 影厅座位详情信息 服务实现类
 * </p>
 *
 * @author 刘存益
 * @since 2023年11月17日
 */
@Service
public class CinemaSeatServiceImpl extends ServiceImpl<CinemaSeatMapper, CinemaSeat> implements CinemaSeatService {

    @Autowired
    private CinemaSeatMapper cinemaSeatMapper;
    @Autowired
    private CinemaHallMapper cinemaHallMapper;

    /**
     * 查询影厅的可用座位
     * @param hallId
     * @return
     */
    @Override
    public List<CinemaSeat> selNormal(Integer hallId) {
        LambdaQueryWrapper<CinemaSeat> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CinemaSeat::getHallId,hallId).eq(CinemaSeat::getSeatState, StateData.SEAT_STATE_NORMAL);
        return cinemaSeatMapper.selectList(wrapper);
    }

    /**
     * 修改影厅座位状态
     * @param form
     */
    @Override
    public void updateSeatState(updateSeatStateForm form) {
        CinemaSeat cinemaSeat = cinemaSeatMapper.selectById(form.getSeatId());
        if (cinemaSeat ==null) {
            throw new CinemaException(3007,"该座位不存在");
        }
        CinemaHall cinemaHall = cinemaHallMapper.selectById(form.getHallId());
        if (cinemaHall==null) throw new CinemaException(3008,"该影厅不存在");
        if (!cinemaSeat.getHallId().equals(cinemaHall.getHallId())) throw new CinemaException(3009,"该座位不在该影厅");
        if (cinemaHall.getHallState().equals(StateData.HALL_STATE_DELETED)) throw new CinemaException(3010,"该影厅已删除");
        CinemaSeat seat = new CinemaSeat();
        seat.setSeatId(cinemaSeat.getSeatId());
        if (form.getSeatState().equals(StateData.SEAT_STATE_DISABLED)){
            if (cinemaSeat.getSeatState().equals(StateData.SEAT_STATE_DISABLED)) throw new CinemaException(3011,"请勿重复禁用");
            //判断是否排场

            seat.setSeatState(0);
            cinemaSeatMapper.updateById(seat);
        } else if (form.getSeatState().equals(StateData.SEAT_STATE_NORMAL)) {
            if (cinemaSeat.getSeatState().equals(StateData.SEAT_STATE_NORMAL)) throw new CinemaException(3012,"请勿重复启用");
            if (cinemaHall.getHallState().equals(StateData.HALL_STATE_DISABLED)) throw new CinemaException(3013,"该影厅已禁用，启用座位失败");
            seat.setSeatState(StateData.SEAT_STATE_NORMAL);
            cinemaSeatMapper.updateById(seat);
        }else {
            throw new CinemaException(3014,"数据不合法");
        }
    }

    /**
     * 根据座位状态查询影厅座位
     * @param form
     * @return
     */
    @Override
    public List<CinemaSeat> selectHallSeat(SelectHallSeatForm form) {
        LambdaQueryWrapper<CinemaSeat> wrapper = new LambdaQueryWrapper<>();
        if (form.getSeatState()!=null) wrapper.eq(CinemaSeat::getSeatState,form.getSeatState());
        wrapper.eq(CinemaSeat::getHallId,form.getHallId());
        return cinemaSeatMapper.selectList(wrapper);
    }

    @SentinelResource(value = "test",blockHandler = "blockTest")
    @Override
    public String test(String s) {
        System.out.println(s);
        System.out.println("hello");
        return "hello";
    }

    public String blockTest(String s, BlockException e){
        System.out.println(e.getClass().getTypeName());
        return "block";
    }


}
